package demo1字节流;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author And(yanqingyuan)
 * @Date 2022/6/25 20:05
 * @Details
 */
public class MyTest {
    private double resultTime=0.0;
    public double method(double sum,double[] times,double[] needTime){
        if(times.length!=needTime.length){
            throw new RuntimeException("参数有误");
        }
        Map<Double,Double> map=new HashMap<>();
        for (int i=0;i<sum;i++){
            //剩余时间作为key
            map.put(needTime[i],times[i]);
        }
        //直到所有的下载都完成，就退出循环
        while(sum>0){
            //递归计算一次时间
            method2(map);
            sum--;
        }
        return resultTime;
    }
    public Map<Double,Double> method2(Map<Double,Double> map){
        Map<Double,Double> newMap=new HashMap<>();
        Set<Double> set = map.keySet();
        double minNeedTime=Double.MAX_VALUE;
        for (Double aDouble : set) {
            if (minNeedTime<aDouble){
                minNeedTime=aDouble;
            }
        }
        if (minNeedTime==Double.MAX_VALUE){
            return null;
        }
        //找到最小的剩余时间以后,加上最小的时间
        resultTime+=minNeedTime;

        Double minValue = map.get(minNeedTime);
        map.remove(minValue);
        //把下载速度提高
        Set<Double> doubles = map.keySet();
        for (Double aDouble : doubles) {
            Double time = map.get(aDouble);
            //下载速度变更
            time+=minValue/doubles.size();
            //剩余时间变更
            Double time2=(aDouble- map.get(aDouble))/time;
            newMap.put(time2,time);
        }
        return newMap;
    }

}
